#!/bin/python3
# -*- coding: utf-8 -*-
# my_BPC1.py
# @author 刘秋
# @email lq@aqiu.info
# @description: 整理文件
# @created 2020-04-07T13:10:35.052Z+08:00
# @last-modified 2020-06-04T13:57:30.361Z+08:00
#

import os
import shutil
import datetime
import numpy as np
import pandas as pd
from setting import Setting


class file_move(object):
    def __init__(self, setting):
        self.path = setting.path
        self.files_list = self.files_list_f(self.path)
        self.__type_dict = setting.type_dict
        self.__DA = setting.DA
        self.files_dict = self.__file_types_list()
        self.__isFolderPath = setting.is_DA

    def set_is_nas(self, value: bool):
        self.__isFolderPath = value

    def get_is_nas(self):
        return self.__isFolderPath

    def files_list_f(self, path):
        """返回传入地址的文件列表"""
        return [
            x for x in os.listdir(path)
            if os.path.isfile(os.path.join(path, x))
        ]

    def __file_types_list(self):
        """对文件进行分类
        
        Returns:
            [type] -- [description]
        """
        '''
        f_a = lambda x: {k: x for k, v in self.__type_dict.items()
        if x.split(".")[-1] in v}
        file_dict = {y:x for z in map(f_a, self.files_list)
                    for x, y in z.items() if not x == None}
        '''
        file_dict = {
            x: k
            for x in self.files_list for k, v in self.__type_dict.items()
            if x.split(".")[-1] in v
        }
        return file_dict

    def _mkedirs_type(self, path_type):
        """新建文件夹
        
        Arguments:
            path_type {path} -- 文件夹地址
        """
        if not os.path.exists(path_type):    # 判断文件是否存在
            os.makedirs(path_type)
            print(f"{path_type}创建成功")

    def run_move(self, folder_path=None):
        """移动文件的列表
        
        Keyword Arguments:
            folder_path {path} -- 目标地址 (default: {self.path})
        """
        if folder_path == None:
            folder_path = self.path
        if self.__isFolderPath:
            for name_file, type_file in self.files_dict.items():
                self.my_os_remove(self.path, name_file, self.__DA[type_file])
        else:
            for name_file, type_file in self.files_dict.items():
                path_type = os.path.join(folder_path, type_file)
                self._mkedirs_type(path_type)
                self.my_os_remove(self.path, name_file, path_type)

    def my_os_remove(self, path, name, folder_path):
        """移动文件
        
        Arguments:
            path {path} -- 原始地址
            name {文件名} -- 要移动的文件名
            folder_path {path} -- 目的地地址
        """
        try:
            shutil.move(os.path.join(path, name), folder_path)
        except shutil.Error as err:
            print(f"{os.path.join(path,name)}->有问题")
            print(err)
            new_time = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
            new_name = str(new_time) + "&" + name
            os.rename(os.path.join(path, name), os.path.join(path, new_name))
            print(f"旧的名字:{name}==>>新的名字：{new_name}")
            self.my_os_remove(path, new_name, folder_path)


if __name__ == "__main__":
    setting = Setting()
    for setting.path in setting.paths:
        file_1 = file_move(setting)
        print(file_1.files_dict)
        file_1.run_move()
        print(file_1.get_is_nas())
